package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.PoiUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author : [chenys]
 * @version : [v1.0]
 * @Description : 预约管理
 * @createTime : [2022/5/24 15:06]
 */
@RestController
@RequestMapping(value = "/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * Excel文件上传，并解析文件内容保存到数据库
     * 不能使用byte[]进行接收，因为在配置文件中进行了解析操作并封装为MultipartFile Java对象
     * @author chenxys
     * @param file
     * @return com.itheima.entity.Result
     */
    @RequestMapping("/upload")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        try {
            //list里面存放了string数组，每个数组里都存放了日期,可预约人数
            List<String[]> list = PoiUtils.readExcel(file);
            if (list.size() > 0){
                List<OrderSetting> orderSettingList = new ArrayList<OrderSetting>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                orderSettingService.add(orderSettingList);
            }
        } catch (IOException e) {
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据月份查询预约数据
     * 参数date的格式为：yyyy-MM
     * @author chenxys
     * @param date
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/getOrderSettingByMonth")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result getOrderSettingByMonth(String date){
        try {
            //返回值格式 [data:日,number:预约人数,reservation:已预约人数]-->使用map进行封装
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,list);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 按日期修改预约
     * @author chenxys
     * @param orderSetting
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/editNumberByDate")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderSettingService.editNumberByDate(orderSetting);
        }catch (Exception e){
            //修改失败
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        //修改成功
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
